﻿using UnityEngine;
using System.Collections;
using Dict.Config;
using Dict.Blo;

namespace UI
{
    public class PropertyTab : MonoBehaviour
    {
        /// <summary>
        /// 人物基础属性
        /// </summary>
        public UILabel heroName;
        public UILabel heroRefine;
        public UILabel heroStar;    //颜色资质
        public UILabel heroLevel;
        public UILabel heroQuality;
        public UILabel heroSpace;
        public UILabel heroAttack;
        public UILabel heroPdef;
        public UILabel heroHp;
        public UILabel heroMdef;

        /// <summary>
        /// 技能list
        /// </summary>
        public GameObject skillItem;
        public GameObject skillRoot;

        public GameObject btnSkillSet;

        UIHero heroInfo;

        /// <summary>
        /// name用于查看其它玩家时传玩家名字
        /// </summary>
        /// <param name="formation"></param>
        /// <param name="name"></param>
        public void SetHero(UIHeroFormation formation, string name = "")
        {
            //Debug.Log( "current prop " +formation.CurrentAttribute);
            heroInfo = formation.hero;
            Dict.DataModel.TableHero heroData = formation.hero.heroData;

            //名字
            heroName.text = heroInfo.heroName;

            if (name != "")
                heroName.text = name;

            //天赋等级
            if(heroInfo.talentLevel > 0)
            {
                heroRefine.text = string.Format("+{0:d}", heroInfo.talentLevel);
            }
            else
            {
                heroRefine.text = "";
            }

            //品质
            heroStar.text = TableTextBlo.GetText(string.Format("quality_name_{0:d}", heroData.quality)).Replace("\\r", "\r").Replace("\\n", "\n");
            heroStar.color = StaticData.playerData.InstPlayerData.GetQualityColor(heroData.quality);

            //等级
            heroLevel.text = heroInfo.level.ToString();

            //品质
            heroQuality.text = heroData.quality.ToString();
            heroSpace.text = "[" + heroInfo.GetHeroSpaceName() + "]";

            //战斗属性
            heroAttack.text = ((int)formation.GetPorp(AttributeQueue.Attack)).ToString();
            heroPdef.text = ((int)formation.GetPorp(AttributeQueue.PhyDefend)).ToString();
            heroHp.text = ((int)formation.GetPorp(AttributeQueue.Hp)).ToString();
            heroMdef.text = ((int)formation.GetPorp(AttributeQueue.MagicDefend)).ToString();

            //初始化属性scrollview
            skillRoot.transform.DestroyChildren();
            float y = 0;
            int count = 0;
            int totalHeight = 0;
            for (int i = 0; i < 3; i++)
            {
                if(heroInfo.Skills[i] != null)
                {
                    GameObject obj = Instantiate(skillItem);
                    obj.GetComponent<SkillItem>().SetSkill(heroInfo.Skills[i],i,heroInfo.breachlevel);
                    obj.transform.parent = skillRoot.transform;
                    obj.transform.localScale = new Vector3(1, 1, 1);
                    obj.transform.localPosition = new Vector3(0, y, 0);
                    y -= obj.GetComponent<SkillItem>().SkillItemHeight;
                    totalHeight += obj.GetComponent<SkillItem>().SkillItemHeight;
                    count++;
                }
            }
            Vector3 size = skillRoot.GetComponent<BoxCollider>().size;
            Vector3 center = skillRoot.GetComponent<BoxCollider>().center;
            skillRoot.GetComponent<BoxCollider>().size = new Vector3(size.x, totalHeight, size.z);
            skillRoot.GetComponent<BoxCollider>().center = new Vector3(center.x, -totalHeight / 2, center.z);

        }
        
        public void OnSkillSetBtn()
        {
            PageManager.Instance.OpenPage("SkillSetting", "guid=" + heroInfo.guid);
        }
    }
}
